package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Periodicals;
import com.company.project.model.ThesisDelivery;
import com.company.project.service.impl.PeriodicalsServiceImpl;
import com.company.project.util.AdminPAndPCRAndDBRAndPE;
import com.company.project.util.FileUploadUtil;
import com.company.project.util.PageUtil;
import com.company.project.util.periodicalsWhere;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.common.BaseMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/periodicals")
public class PeriodicalsController {
    @Resource
    @Autowired
    private PeriodicalsServiceImpl periodicalsService;

    @Resource
    private FileUploadUtil fileUploadUtil;

    /**
     * 前台搜索框搜索期刊
     * @param cname
     * @param index
     * @return
     */
    @PostMapping("/queryPM/{index}")
    @ResponseBody
    public PageUtil queryPM(@RequestBody String cname, @PathVariable("index") Integer index){
        PageUtil<Map<Object,Object>> periodicalsPageUtil = periodicalsService.queryPM(cname, index);
        return periodicalsPageUtil;
    }
    //根据id查询期刊详情
    @RequestMapping(value = "/selectPeriodicalsById/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Periodicals selectPeriodicalsById (@PathVariable("id") String id){
        return periodicalsService.selectPeriodicalsById(id);
    }

    //获取优秀期刊，分页
    @RequestMapping(value = "/periodicalsExcellence/{index}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public PageUtil<Periodicals> selectPeriodicalsExcellence(@PathVariable(value = "index") Integer index){
        return  periodicalsService.selectPeriodicalsExcellence(index);
    }

    //首页期刊轮播图
    @RequestMapping(value = "/periodicalsExcellenceImg",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<Periodicals> selectPeriodicalsExcellence(){
        return  periodicalsService.getPeriodicalsExcellenceImg();
    }

    @PostMapping("/add")
    public Result add(Periodicals periodicals) {
        periodicalsService.save(periodicals);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        periodicalsService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Periodicals periodicals) {
        periodicalsService.update(periodicals);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Periodicals periodicals = periodicalsService.findById(id);
        return ResultGenerator.genSuccessResult(periodicals);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Periodicals> list = periodicalsService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    //卫生期刊
    @RequestMapping(value="/list/where/{index}",method=RequestMethod.POST)
    @ResponseBody
    public PageUtil listByWhere(@RequestBody(required = false) periodicalsWhere pw,@PathVariable Integer index) {
        return  periodicalsService.getPeriodicalsByWhere(pw,index);
    }

    //根据id查询name
    @RequestMapping(value="/name/{id}",method=RequestMethod.GET)
    @ResponseBody
    public String selectNameById(@PathVariable(value = "id",required = false) Integer id) {
        if (id==null && ("").equals(id)){
            return "";
        }
        return  periodicalsService.getPeriodicalsName(id);
    }

    //后台查询所有期刊
    @RequestMapping(value="/admin/list/{index}",method = RequestMethod.GET)
    public Map<String,Object> andminSelectPredicateList(@PathVariable(value = "index") Integer index){
        return periodicalsService.adminGetPeriodicalsList(index);
    }

    //后台查询单个期刊
    @RequestMapping(value="/admin/{id}",method = RequestMethod.GET)
    public Periodicals andminGetPredicates(@PathVariable(value = "id") Integer id){
        return periodicalsService.adminGettPeriodicals(id);
    }

    //后台添加期刊
    @PostMapping("/admin/add/periodicals")
    public Result adminAddPeriodicals(@RequestParam String apd,MultipartFile file,MultipartFile qrFile){
        AdminPAndPCRAndDBRAndPE apds = JSON.parseObject(apd,AdminPAndPCRAndDBRAndPE.class);
        if(file!=null){
           String result=fileUploadUtil.writeFile(file,"img");
            if(result==null){
                return ResultGenerator.getInternalServerErrot("期刊封面图片上传错误");
            }
            apds.getPeriodicals().setPhoto(result);
        }
        if(qrFile!=null){
            String result=fileUploadUtil.writeFile(qrFile,"img");
            if(result==null){
                return ResultGenerator.getInternalServerErrot("期刊二维码图片上传错误");
            }
            apds.getPeriodicals().setQrUri(result);
        }
        boolean p = periodicalsService.adminAddPeriodicals(apds);
        if(p){
            return ResultGenerator.genSuccessResult("保存成功");
        }
        return ResultGenerator.getInternalServerErrot("保存错误");
    }

    //后台修改期刊
    @PostMapping("/admin/update/periodicals")
    public Result adminUpdatePeriodicals(@RequestParam String apd,MultipartFile file,MultipartFile qrFile){
        AdminPAndPCRAndDBRAndPE apds = JSON.parseObject(apd,AdminPAndPCRAndDBRAndPE.class);
        if(file!=null){
            String result=fileUploadUtil.writeFile(file,"img");
            if(result==null){
                return ResultGenerator.getInternalServerErrot("期刊封面图片上传错误");
            }
            apds.getPeriodicals().setPhoto(result);
        }

        if(qrFile!=null){
            String result=fileUploadUtil.writeFile(qrFile,"img");
            if(result==null){
                return ResultGenerator.getInternalServerErrot("期刊二维码图片上传错误");
            }
            apds.getPeriodicals().setQrUri(result);
        }
        boolean p =  periodicalsService.adminUpdatePeriodicals(apds);
        if(p){
            return ResultGenerator.genSuccessResult("保存成功");
        }
        return ResultGenerator.getInternalServerErrot("保存错误");
    }

    //后台删除期刊
    @GetMapping("/admin/delect/{id}")
    public boolean adminDelectPeriodicals(@PathVariable(value = "id") Integer id){
       return   periodicalsService.adminDelectPeriodicals(id);
    }

    //后台修改期刊状态
    @PostMapping("/admin/update/status")
    public boolean adminUpdatePeriodicals(@RequestBody Periodicals periodicals){
        return periodicalsService.adminUpdateStatus(periodicals);
    }
}
